#!/usr/bin/make -f

SHELL    := sh -e
SOURCE := $(shell dpkg-parsechangelog -SSource)
VERSION := $(shell dpkg-parsechangelog -SVersion)
VERSION_UPSTREAM := $(shell echo "$(VERSION)" | sed -e 's,-[^-]*$$,,')
VERSION_BINNMU := $(shell echo "$(VERSION)" | sed -rne 's,.*\+b([0-9]+)$$,\1,p')
VERSION_SOURCE := $(patsubst %+b$(VERSION_BINNMU),%,$(VERSION))

include debian/rules.defs

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  DEBIAN_KERNEL_JOBS := $(subst parallel=,,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif
ifdef DEBIAN_KERNEL_JOBS
  MAKEFLAGS += -j$(DEBIAN_KERNEL_JOBS)
endif

# dpkg-buildpackage passes the -s option if DEB_BUILD_OPTIONS contains
# terse, but that triggers Kbuild to turn off progress messages
# completely, which we don't want.
override MAKEFLAGS := $(subst s,,$(filter-out -%,$(MAKEFLAGS))) $(filter -%,$(MAKEFLAGS))

.NOTPARALLEL:

source: debian/control
	dh_testdir
	$(MAKE) -f debian/rules.gen source

setup: debian/control
	dh_testdir
	$(MAKE) -f debian/rules.gen setup_$(DEB_HOST_ARCH)

build: build-arch build-indep

build-arch: debian/control
	dh_testdir
# The perf-read-vdso* programs are built for different architectures,
# without standard flags, but are not exposed to untrusted input.
# Kernel code needs different hardening options that blhc doesn't know
# about.
# We need to use terse builds in CI due to the log size limit.  This
# mostly affects the output for builds of kernel code, which need
# different options for hardening anyway.
	@printf '%s\n' 'blhc: ignore-line-regexp: (.* -o *[^ ]*/perf-read-vdso.*|.* -D__KERNEL__ .*$(if $(filter terse,$(DEB_BUILD_OPTIONS)),| *(CC(LD)?|LD|LINK)\b.*))'
	$(MAKE) -f debian/rules.gen build-arch_$(DEB_HOST_ARCH)

build-indep: debian/control
	dh_testdir
	$(MAKE) -f debian/rules.gen build-indep

binary:	binary-indep binary-arch

binary-arch: build-arch
	dh_testdir
	$(MAKE) -f debian/rules.gen binary-arch_$(DEB_HOST_ARCH)

binary-indep: build-indep
	dh_testdir
	$(MAKE) -f debian/rules.gen binary-indep

DIR_ORIG = ../orig/$(SOURCE)-$(VERSION_UPSTREAM)
TAR_ORIG_NAME = $(SOURCE)_$(VERSION_UPSTREAM).orig.tar.xz
TAR_ORIG = $(firstword $(wildcard ../$(TAR_ORIG_NAME)) $(wildcard ../orig/$(TAR_ORIG_NAME)))

orig: $(DIR_ORIG)
	rsync --delete --exclude /debian --exclude .svk --exclude .svn --exclude .git --link-dest=$(DIR_ORIG)/ -a $(DIR_ORIG)/ .
	QUILT_PATCHES='$(CURDIR)/debian/patches' QUILT_PC=.pc quilt push --quiltrc - -a -q --fuzz=0

$(DIR_ORIG):
ifeq ($(TAR_ORIG),)
	$(error Cannot find orig tarball $(TAR_ORIG_NAME))
else
	mkdir -p ../orig
	tar -C ../orig -xaf $(TAR_ORIG)
endif

CLEAN_PATTERNS := $(BUILD_DIR) $(STAMPS_DIR) debian/lib/python/debian_linux/*.pyc debian/lib/python/debian_linux/__pycache__ $$(find debian -maxdepth 1 -type d -name 'linux-*') debian/*-modules-*-di* debian/kernel-image-*-di* debian/*-tmp debian/*.substvars

maintainerclean:
	rm -rf $(CLEAN_PATTERNS)
# We cannot use dh_clean here because it requires debian/control to exist
	rm -rf debian/.debhelper debian/*.debhelper* debian/files
	rm -f \
		debian/linux-image-*.NEWS \
		debian/linux-*.bug-presubj \
		debian/linux-*.lintian-overrides \
		debian/linux-*.maintscript \
		debian/linux-*.postinst \
		debian/linux-*.postrm \
		debian/linux-*.preinst \
		debian/linux-*.prerm \
		debian/config.defines.dump \
		debian/control \
		debian/control.md5sum \
		debian/rules.gen \
		debian/tests/control
	rm -rf $(filter-out debian .svk .svn .git, $(wildcard * .[^.]*))

clean: debian/control
	dh_testdir
	rm -rf $(CLEAN_PATTERNS)
	dh_clean

CONTROL_FILES = $(BUILD_DIR)/version-info $(wildcard debian/templates/*.in)
CONTROL_FILES += debian/config/defines $(wildcard debian/config/*/defines) $(wildcard debian/config/*/*/defines)
CONTROL_FILES += debian/installer/kernel-versions debian/installer/package-list

# debian/bin/gencontrol.py uses debian/changelog as input, but the
# output only depends on the source name and version.  To avoid
# frequent changes to debian/control.md5sum, include only those fields
# in the checksum.
$(BUILD_DIR)/version-info: debian/changelog
	mkdir -p $(@D)
	printf >$@ 'Source: %s\nVersion: %s\n' $(SOURCE) $(VERSION_SOURCE)

debian/control debian/rules.gen: debian/bin/gencontrol.py $(CONTROL_FILES)
ifeq ($(wildcard debian/control.md5sum),)
	$(MAKE) -f debian/rules debian/control-real
else
	md5sum --check debian/control.md5sum --status || \
		$(MAKE) -f debian/rules debian/control-real
endif

debian/control-real: debian/bin/gencontrol.py $(CONTROL_FILES)
# Hash randomisation makes the pickled config unreproducible
	PYTHONHASHSEED=0 $<
	md5sum $^ > debian/control.md5sum
	@echo
	@echo This target is made to fail intentionally, to make sure
	@echo that it is NEVER run during the automated build. Please
	@echo ignore the following error, the debian/control file has
	@echo been generated SUCCESSFULLY.
	@echo
	exit 1

.PHONY: binary binary-% build build-% clean debian/control-real orig setup source
